﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：SV测试
    /// </summary>
    public class R022 : BaseReport
    {
        public override void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
            #region 表头
            int headColumnsIndex = 0;

            for (int i = 0; i < 1; i++)
            {
                try
                {
                    sheet.GetRow(14).GetCell(headColumnsIndex + 0).SetCellValue(
                        string.Format("{0} {1}uF/{2}V {3}"
                        , head.Series
                        , head.Cap.ToString()
                        , head.Voltage.ToString()
                        , head.Size));
                    sheet.GetRow(14).GetCell(headColumnsIndex + 3).SetCellValue(head.LotNo);
                    sheet.GetRow(14).GetCell(headColumnsIndex + 5).SetCellValue(head.CustomerPn);
                    sheet.GetRow(14).GetCell(headColumnsIndex + 8).SetCellValue(head.Pn);
                    sheet.GetRow(14).GetCell(headColumnsIndex + 11).SetCellValue(head.DateCode);

                    sheet.GetRow(31).GetCell(headColumnsIndex + 5).SetCellValue(
                    string.Format("温度： {0} ℃      湿度：{1} %", head.TestTemp, head.TestHumidity));

                    sheet.GetRow(32).GetCell(headColumnsIndex + 6).SetCellValue(head.ApproveUser);
                    sheet.GetRow(32).GetCell(headColumnsIndex + 10).SetCellValue(head.CreateUser);
                    sheet.GetRow(32).GetCell(headColumnsIndex + 13).SetCellValue(head.TestDate.ToString("yyyy/M/d"));


                    if (head.IsValid)
                        sheet.GetRow(32).GetCell(headColumnsIndex + 2).SetCellValue("OK");
                    else
                        sheet.GetRow(32).GetCell(headColumnsIndex + 2).SetCellValue("NG");
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }
            }
            #endregion 表头

            #region 细表
            int currentDataColumnIndex = 1;
            int dataRowIndex = 18;

            sheet.GetRow(17).GetCell(headColumnsIndex + 1).SetCellValue(head.Standard.CapMin + "~" + 
                head.Standard.CapMax);
            sheet.GetRow(17).GetCell(headColumnsIndex + 2).SetCellValue(Math.Round(head.DfMax/100,3) + "Max");
            sheet.GetRow(17).GetCell(headColumnsIndex + 3).SetCellValue(head.LcMax + "Max");
            sheet.GetRow(17).GetCell(headColumnsIndex + 4).SetCellValue(head.EsrMax + "(Ω)Max");

            sheet.GetRow(17).GetCell(headColumnsIndex + 6).SetCellValue("±" + head.CapRate + "%");
            sheet.GetRow(17).GetCell(headColumnsIndex + 7).SetCellValue(head.DfMaxAfter / 100d + "Max");
            sheet.GetRow(17).GetCell(headColumnsIndex + 9).SetCellValue(head.LcMax + "Max");
            sheet.GetRow(17).GetCell(headColumnsIndex + 10).SetCellValue(head.EsrMaxAfter + "(Ω)Max");

            try
            {
                #region data

                if (head.Dtls.Count > 0)
                {
                    int cIndex = currentDataColumnIndex;
                    var caps = head.Dtls[0].Datas.Where(d => d.ItemCode == "CAP");
                    if (caps.Any())
                    {
                        var carpss = caps.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = carpss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetCapNumFormat(maxlc));
                        //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                        for (int k = 0; k < carpss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (carpss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    cIndex++;
                    var dfs = head.Dtls[0].Datas.Where(d => d.ItemCode == "DF");
                    if (dfs.Any())
                    {
                        var dfss = dfs.ToList();
                        for (int k = 0; k < dfss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (dfss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(Math.Round(dfss[k].Value / 100, 4));
                        }
                    }

                    cIndex++;
                    var lcs = head.Dtls[0].Datas.Where(d => d.ItemCode == "LC");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = lcss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    cIndex++;
                    lcs = head.Dtls[0].Datas.Where(d => d.ItemCode == "ESR");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = lcss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                }

                if (head.Dtls.Count > 1)
                {
                    int cIndex = 5;
                    var caps = head.Dtls[1].Datas.Where(d => d.ItemCode == "CAP");
                    if (caps.Any())
                    {
                        var carpss = caps.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = carpss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat("0.00");
                        //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                        for (int k = 0; k < carpss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (carpss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    cIndex = 7;
                    var dfs = head.Dtls[0].Datas.Where(d => d.ItemCode == "DF");
                    if (dfs.Any())
                    {
                        var dfss = dfs.ToList();
                        for (int k = 0; k < dfss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (dfss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(Math.Round(dfss[k].Value / 100, 3));
                        }
                    }

                    cIndex = 9;
                    var lcs = head.Dtls[0].Datas.Where(d => d.ItemCode == "LC");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = lcss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    cIndex++;
                    lcs = head.Dtls[0].Datas.Where(d => d.ItemCode == "ESR");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = lcss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                }
                #endregion data

            }
            catch (Exception ex)
            {
                Logger.LogError(ex);

            }

            #endregion

            sheet.ForceFormulaRecalculation = true;
        }
    }
}
